package com.youhome.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.oss.OSSClient;
import com.youhome.config.OSSConfig;
import com.youhome.mapper.*;
import com.youhome.model.*;
import com.youhome.result.*;
import com.youhome.result.School;
import com.youhome.service.HomeJunctionService;
import com.youhome.utils.OSSUtil;
import net.sf.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chen365 on 2018/06/14.
 */
@Service
public class HomeJunctionServiceImpl implements HomeJunctionService {
    @Autowired
    private OSSConfig ossConfig;
    @Autowired
    private HouseMapper listHubMapper;
    @Autowired
    private HJStateMapper hjStateMapper;
    @Autowired
    private HJCityMapper hjCityMapper;
    @Autowired
    private HJZipCodeMapper hjZipCodeMapper;
    @Autowired
    private HJNeighborhoodMapper hjNeighborhoodMapper;
    @Autowired
    private HJCountyMapper hjCountyMapper;
    @Autowired
    private HJSchoolMapper hjSchoolMapper;
    @Autowired
    private HJPOICategoryMapper hjpoiCategoryMapper;
    @Autowired
    private HJSchoolAttendanceAreaMapper hjSchoolAttendanceAreaMapper;


    public static void main(String[] args) {


    }

    @Override
    public HomeJunction periphery(String houseId) {
        HomeJunction homeJunction = new HomeJunction();
        House listHub = listHubMapper.selectByPrimaryKey(houseId);
        if (listHub != null) {

            String key = "json/" + listHub.getListingKey() + ".json";

            String state_name = listHub.getProvince();
            String city_name = listHub.getCity();
            String county_name = listHub.getCounty();
            String neighborhood_name = null;
            String zip_name = listHub.getZip();

            HJState hjState = new HJState();
            HJCounty hjCounty = new HJCounty();
            HJCity hjCity = new HJCity();
            HJNeighborhood hjNeighborhood = new HJNeighborhood();

            BaseArea stateBaseArea = new BaseArea();
            BaseArea cityBaseArea = new BaseArea();
            BaseArea countyBaseArea = new BaseArea();
            BaseArea neighborhoodBaseArea = new BaseArea();
            BaseArea zipBaseArea = new BaseArea();

            OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
            String json = OSSUtil.downloadJson(ossClient, ossConfig.getBacketName(), key);

            if (!StringUtils.isEmpty(state_name)) {
                hjState = hjStateMapper.selectByAbbreviation(state_name);
                if (hjState != null) {
                    String state_json_name = "homejunction/state/" + hjState.getId() + ".json";
                    stateBaseArea = getBaseArea(state_json_name);
                }
            }

            if (!StringUtils.isEmpty(city_name) && hjState != null) {
                HJCity city_record = new HJCity();
                city_record.setName(city_name);
                city_record.setStateId(hjState.getId());

                hjCity = hjCityMapper.selectByBean(city_record);
                if (hjCity != null) {
                    String city_json_name = "homejunction/city/" + hjCity.getId() + ".json";
                    cityBaseArea = getBaseArea(city_json_name);
                }
            }

            if (!StringUtils.isEmpty(county_name) && hjState != null) {
                HJCounty county_record = new HJCounty();
                county_record.setStateId(hjState.getId());
                county_record.setName(listHub.getCounty());
                hjCounty = hjCountyMapper.selectByBean(county_record);
                if (hjCounty != null) {
                    String county_json_name = "homejunction/county/" + hjCounty.getId() + ".json";
                    countyBaseArea = getBaseArea(county_json_name);
                }
            }

            if (!StringUtils.isEmpty(zip_name)) {
                HJZipCode hjZipCode = hjZipCodeMapper.selectByZipCode(zip_name);
                if (hjZipCode != null) {
                    String zip_json_name = "homejunction/zip/" + hjZipCode.getId() + ".json";
                    zipBaseArea = getBaseArea(zip_json_name);
                }
            }

            if (!StringUtils.isEmpty(json) && hjState != null && hjCity != null && hjCounty != null) {
                JSONObject dataJson = JSONObject.parseObject(json);
                if (dataJson.get("location") != null && !dataJson.get("location").equals("null")) {
                    JSONObject location = JSONObject.parseObject(dataJson.get("location").toString());
                    if (location.get("neighborhoods") != null) {
                        JSONObject neighborhoods = JSONObject.parseObject(location.get("neighborhoods").toString());
                        if (neighborhoods != null) {
                            //JSONObject neighborhood = JSONObject.parseObject(neighborhoods.get("neighborhood").toString());
                            JSONArray neighborhoodss = JSONArray.parseArray(neighborhoods.get("neighborhood").toString());
                            if (neighborhoodss.size() > 0) {
                                JSONObject neighborhood = JSONObject.parseObject(neighborhoodss.get(0).toString());
                                if (neighborhood.get("name") != null) {
                                    neighborhood_name = neighborhood.get("name").toString();
                                    HJNeighborhood neighborhood_record = new HJNeighborhood();
                                    neighborhood_record.setName(neighborhood_name);
                                    neighborhood_record.setStateId(hjState.getId());
                                    neighborhood_record.setCityId(hjCity.getId());
                                    neighborhood_record.setCountyId(hjCounty.getId());

                                    hjNeighborhood = hjNeighborhoodMapper.selectByBean(neighborhood_record);
                                    if (hjNeighborhood != null) {
                                        String neighbourhood_json_name = "homejunction/neighborhood/" + hjNeighborhood.getId() + ".json";
                                        neighborhoodBaseArea = getBaseArea(neighbourhood_json_name);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            homeJunction.setIncome(getIncome(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setCosts(getCosts(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setWeather(getWeather(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setHouseholds(getHouseholds(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setEmployment(getEmployment(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setEducation(getEducation(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setCrime(getCrime(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setRace(getRace(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setArea(getArea(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setAge(getAge(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setPopulation(getPopulation(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));
            homeJunction.setGender(getGender(stateBaseArea, cityBaseArea, countyBaseArea, neighborhoodBaseArea, zipBaseArea));

        }


        return homeJunction;
    }

    @Override
    public School school(String zip) {

        List<HJSchool> hjSchools = hjSchoolMapper.selectByZip(zip);
        School school = new School();
        List<SchoolItem> elementary = new ArrayList<>();
        List<SchoolItem> middle = new ArrayList<>();
        List<SchoolItem> high = new ArrayList<>();

        if (hjSchools != null && hjSchools.size() > 0) {
            for (HJSchool hjSchool : hjSchools) {
                SchoolItem schoolItem = toSchoolItem(hjSchool);

                if (hjSchool.getHasElementary() != null && hjSchool.getHasElementary()) {
                    elementary.add(schoolItem);
                }
                if (hjSchool.getHasMiddle() != null && hjSchool.getHasMiddle()) {
                    middle.add(schoolItem);
                }
                if (hjSchool.getHasHigh() != null && hjSchool.getHasHigh()) {
                    high.add(schoolItem);
                }
            }
        }

        if (elementary.size() > 0) {
            school.setElementary(elementary);
        }
        if (high.size() > 0) {
            school.setHigh(high);
        }
        if (middle.size() > 0) {
            school.setMiddle(middle);
        }

        return school;
    }

    @Override
    public HJZipCode selectZipByZipCode(String zipCode) {
        return hjZipCodeMapper.selectByZipCode(zipCode);
    }

    @Override
    public HJCity selectCityByLabel(String label) {
        return hjCityMapper.selectByLabel(label);
    }

    @Override
    public HJState selectStateByAbbreviation(String abbreviation) {
        return hjStateMapper.selectByAbbreviation(abbreviation);
    }

    @Override
    public SchoolDetail schoolDetail(String id) {

        String key = "homejunction/school/" + id + ".json";

        OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        String json = OSSUtil.downloadJson(ossClient, ossConfig.getBacketName(), key);

        if (!StringUtils.isEmpty(json)) {
            JSONObject dataJson = JSONObject.parseObject(json);

            SchoolDetail detail = new SchoolDetail();
            if (dataJson.get("totalStudents") != null)
                detail.setTotalStudents((Integer) dataJson.get("totalStudents"));
            if (dataJson.get("totalTeachers") != null)
                detail.setTotalTeachers((Integer) dataJson.get("totalTeachers"));
            if (dataJson.get("studentTeacherRatio") != null)
                detail.setStudentTeacherRatio((String) dataJson.get("studentTeacherRatio"));
            if (dataJson.get("enrollment") != null) {
                Enrollment enrollment = new Enrollment();
                JSONObject jsonObject = JSONObject.parseObject(dataJson.get("enrollment").toString());
                if (jsonObject.get("pk") != null)
                    enrollment.setPk((Integer) jsonObject.get("pk"));
                if (jsonObject.get("kg") != null)
                    enrollment.setKg((Integer) jsonObject.get("kg"));
                if (jsonObject.get("1") != null)
                    enrollment.setY1((Integer) jsonObject.get("1"));
                if (jsonObject.get("2") != null)
                    enrollment.setY2((Integer) jsonObject.get("2"));
                if (jsonObject.get("3") != null)
                    enrollment.setY3((Integer) jsonObject.get("3"));
                if (jsonObject.get("4") != null)
                    enrollment.setY4((Integer) jsonObject.get("4"));
                if (jsonObject.get("5") != null)
                    enrollment.setY5((Integer) jsonObject.get("5"));
                if (jsonObject.get("6") != null)
                    enrollment.setY6((Integer) jsonObject.get("6"));
                if (jsonObject.get("7") != null)
                    enrollment.setY7((Integer) jsonObject.get("7"));
                if (jsonObject.get("8") != null)
                    enrollment.setY8((Integer) jsonObject.get("8"));
                if (jsonObject.get("9") != null)
                    enrollment.setY9((Integer) jsonObject.get("9"));
                if (jsonObject.get("10") != null)
                    enrollment.setY10((Integer) jsonObject.get("10"));
                if (jsonObject.get("11") != null)
                    enrollment.setY11((Integer) jsonObject.get("11"));
                if (jsonObject.get("12") != null)
                    enrollment.setY12((Integer) jsonObject.get("12"));

                detail.setEnrollment(enrollment);
            }

            if (dataJson.get("demographics") != null) {
                Demographics demographics = new Demographics();
                JSONObject jsonObject = JSONObject.parseObject(dataJson.get("demographics").toString());
                if (jsonObject.get("asian") != null)
                    demographics.setAsian((Integer) jsonObject.get("asian"));
                if (jsonObject.get("black") != null)
                    demographics.setBlack((Integer) jsonObject.get("black"));
                if (jsonObject.get("hispanic") != null)
                    demographics.setHispanic((Integer) jsonObject.get("hispanic"));
                if (jsonObject.get("native") != null)
                    demographics.setNatives((Integer) jsonObject.get("native"));
                if (jsonObject.get("pacific") != null)
                    demographics.setPacific((Integer) jsonObject.get("pacific"));
                if (jsonObject.get("white") != null)
                    demographics.setWhite((Integer) jsonObject.get("white"));
                if (jsonObject.get("female") != null)
                    demographics.setFemale((Integer) jsonObject.get("female"));
                if (jsonObject.get("freeLunch") != null)
                    demographics.setFreeLunch((Integer) jsonObject.get("freeLunch"));
                if (jsonObject.get("reducedLunch") != null)
                    demographics.setReducedLunch((Integer) jsonObject.get("reducedLunch"));
                if (jsonObject.get("male") != null)
                    demographics.setMale((Integer) jsonObject.get("male"));
                if (jsonObject.get("migrant") != null)
                    demographics.setMigrant((Integer) jsonObject.get("migrant"));
                detail.setDemographics(demographics);
            }

            if (dataJson.get("charter") != null)
                detail.setCharter((Boolean) dataJson.get("charter"));
            if (dataJson.get("magnet") != null)
                detail.setMagnet((Boolean) dataJson.get("magnet"));
            if (dataJson.get("titleI") != null)
                detail.setTitleI((Boolean) dataJson.get("titleI"));
            if (dataJson.get("library") != null)
                detail.setLibrary((Boolean) dataJson.get("library"));
            if (dataJson.get("advancedPlacement") != null)
                detail.setAdvancedPlacement((Boolean) dataJson.get("advancedPlacement"));
            return detail;
        }

        return null;
    }

    @Override
    public List<HJPOICategory> selectAllHJPOICategory() {
        return hjpoiCategoryMapper.selectAll();
    }

    @Override
    public List<HJSchool> selectSchoolByZip(String zip) {
        return hjSchoolMapper.selectByZip(zip);
    }

    @Override
    public List<HJSchoolAttendanceArea> selectBySchoolId(String schoolId) {
        return hjSchoolAttendanceAreaMapper.selectBySchoolId(schoolId);
    }

    public SchoolItem toSchoolItem(HJSchool hjSchool) {

        if (hjSchool != null) {
//            String json = JSON.toJSONString(hjSchool);
//            if (!StringUtils.isEmpty(json)) {
//                SchoolItem schoolItem = JSON.parseObject(json, SchoolItem.class);
//                return schoolItem;
//            }

            List<String> address_list = new ArrayList<>();
            address_list.add(hjSchool.getStreet());
            address_list.add(hjSchool.getCity());
            address_list.add(hjSchool.getState());
            address_list.add(hjSchool.getZip());

            String address = String.join(", ", address_list);
            SchoolItem schoolItem = new SchoolItem();
            schoolItem.setId(hjSchool.getId());
            schoolItem.setName(hjSchool.getName());
            schoolItem.setAddress(address);
            schoolItem.setPhone(hjSchool.getPhone());
            schoolItem.setAdmission(hjSchool.getAdmission());
            schoolItem.setGrade(hjSchool.getGradeLow() + "-" + hjSchool.getGradeHigh());
            schoolItem.setReviewRating(hjSchool.getReviewRating());
            return schoolItem;
        }
        return null;
    }

    public BaseArea getBaseArea(String jsonName) {
        BaseArea baseArea = new BaseArea();
        try {
            OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());

            String json = OSSUtil.downloadJson(ossClient, ossConfig.getBacketName(), jsonName);
            if (!StringUtils.isEmpty(json)) {
                JSONObject dataJson = JSON.parseObject(json);

                if (!StringUtils.isEmpty(dataJson.get("age"))) {
                    String item = dataJson.get("age").toString();
                    AgeItem age = JSON.parseObject(item, new TypeReference<AgeItem>() {
                    });
                    if (age != null)
                        baseArea.setAge(age);
                }
                if (!StringUtils.isEmpty(dataJson.get("area"))) {
                    String item = dataJson.get("area").toString();
                    AreaItem area = JSON.parseObject(item, new TypeReference<AreaItem>() {
                    });
                    if (area != null)
                        baseArea.setArea(area);
                }
                if (!StringUtils.isEmpty(dataJson.get("gender"))) {
                    String item = dataJson.get("gender").toString();
                    GenderItem gender = JSON.parseObject(item, new TypeReference<GenderItem>() {
                    });
                    if (gender != null)
                        baseArea.setGender(gender);
                }
                if (!StringUtils.isEmpty(dataJson.get("race"))) {
                    String item = dataJson.get("race").toString();
                    JSONObject jsonObject = JSON.parseObject(item);
                    jsonObject.put("natives", jsonObject.get("native"));
                    jsonObject.remove("native");
                    RaceItem race = JSON.parseObject(jsonObject.toString(), new TypeReference<RaceItem>() {
                    });
                    if (race != null)
                        baseArea.setRace(race);
                }
                if (!StringUtils.isEmpty(dataJson.get("crime"))) {
                    String item = dataJson.get("crime").toString();
                    CrimeItem crime = JSON.parseObject(item, new TypeReference<CrimeItem>() {
                    });
                    if (crime != null)
                        baseArea.setCrime(crime);
                }
                if (!StringUtils.isEmpty(dataJson.get("education"))) {
                    String item = dataJson.get("education").toString();
                    EducationItem education = JSON.parseObject(item, new TypeReference<EducationItem>() {
                    });
                    if (education != null)
                        baseArea.setEducation(education);
                }
                if (!StringUtils.isEmpty(dataJson.get("employment"))) {
                    String item = dataJson.get("employment").toString();
                    EmploymentItem employment = JSON.parseObject(item, new TypeReference<EmploymentItem>() {
                    });
                    if (employment != null)
                        baseArea.setEmployment(employment);
                }
                if (!StringUtils.isEmpty(dataJson.get("households"))) {
                    String item = dataJson.get("households").toString();
                    HouseholdsItem households = JSON.parseObject(item, new TypeReference<HouseholdsItem>() {
                    });
                    if (households != null)
                        baseArea.setHouseholds(households);
                }
                if (!StringUtils.isEmpty(dataJson.get("weather"))) {
                    String item = dataJson.get("weather").toString();
                    WeatherItem weather = JSON.parseObject(item, new TypeReference<WeatherItem>() {
                    });
                    if (weather != null)
                        baseArea.setWeather(weather);
                }
                if (!StringUtils.isEmpty(dataJson.get("income"))) {
                    String item = dataJson.get("income").toString();
                    IncomeItem income = JSON.parseObject(item, new TypeReference<IncomeItem>() {
                    });
                    if (income != null)
                        baseArea.setIncome(income);
                }
                if (!StringUtils.isEmpty(dataJson.get("costs"))) {
                    String item = dataJson.get("costs").toString();
                    CostsItem costs = JSON.parseObject(item, new TypeReference<CostsItem>() {
                    });
                    if (costs != null)
                        baseArea.setCosts(costs);
                }
                if (!StringUtils.isEmpty(dataJson.get("population"))) {
                    String item = dataJson.get("population").toString();
                    JSONObject jsonObject = JSON.parseObject(item);
                    jsonObject.put("y1990", jsonObject.get("1990"));
                    jsonObject.put("y2000", jsonObject.get("2000"));
                    jsonObject.put("y2010", jsonObject.get("2010"));
                    jsonObject.remove("1990");
                    jsonObject.remove("2000");
                    jsonObject.remove("2010");
                    PopulationItem population = JSON.parseObject(jsonObject.toString(), new TypeReference<PopulationItem>() {
                    });
                    if (population != null)
                        baseArea.setPopulation(population);
                }
            } else {
                return null;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return baseArea;
    }

    public District getIncome(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getIncome() != null) district.setState(state.getIncome());
        if (city.getIncome() != null) district.setCity(city.getIncome());
        if (county.getIncome() != null) district.setCounty(county.getIncome());
        if (neighborhood.getIncome() != null) district.setNeighbourhood(neighborhood.getIncome());
        if (zip.getIncome() != null) district.setZip(zip.getIncome());
        return district;
    }

    public District getCosts(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getCosts() != null) district.setState(state.getCosts());
        if (city.getCosts() != null) district.setCity(city.getCosts());
        if (county.getCosts() != null) district.setCounty(county.getCosts());
        if (neighborhood.getCosts() != null) district.setNeighbourhood(neighborhood.getCosts());
        if (zip.getCosts() != null) district.setZip(zip.getCosts());
        return district;
    }

    public District getWeather(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getWeather() != null) district.setState(state.getWeather());
        if (city.getWeather() != null) district.setCity(city.getWeather());
        if (county.getWeather() != null) district.setCounty(county.getWeather());
        if (neighborhood.getWeather() != null) district.setNeighbourhood(neighborhood.getWeather());
        if (zip.getWeather() != null) district.setZip(zip.getWeather());
        return district;
    }

    public District getHouseholds(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getHouseholds() != null) district.setState(state.getHouseholds());
        if (city.getHouseholds() != null) district.setCity(city.getHouseholds());
        if (county.getHouseholds() != null) district.setCounty(county.getHouseholds());
        if (neighborhood.getHouseholds() != null) district.setNeighbourhood(neighborhood.getHouseholds());
        if (zip.getHouseholds() != null) district.setZip(zip.getHouseholds());
        return district;
    }

    public District getEmployment(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getEmployment() != null) district.setState(state.getEmployment());
        if (city.getEmployment() != null) district.setCity(city.getEmployment());
        if (county.getEmployment() != null) district.setCounty(county.getEmployment());
        if (neighborhood.getEmployment() != null) district.setNeighbourhood(neighborhood.getEmployment());
        if (zip.getEmployment() != null) district.setZip(zip.getEmployment());
        return district;
    }

    public District getEducation(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getEducation() != null) district.setState(state.getEducation());
        if (city.getEducation() != null) district.setCity(city.getEducation());
        if (county.getEducation() != null) district.setCounty(county.getEducation());
        if (neighborhood.getEducation() != null) district.setNeighbourhood(neighborhood.getEducation());
        if (zip.getEducation() != null) district.setZip(zip.getEducation());
        return district;
    }

    public District getCrime(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getCrime() != null) district.setState(state.getCrime());
        if (city.getCrime() != null) district.setCity(city.getCrime());
        if (county.getCrime() != null) district.setCounty(county.getCrime());
        if (neighborhood.getCrime() != null) district.setNeighbourhood(neighborhood.getCrime());
        if (zip.getCrime() != null) district.setZip(zip.getCrime());
        return district;
    }

    public District getRace(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getRace() != null) district.setState(state.getRace());
        if (city.getRace() != null) district.setCity(city.getRace());
        if (county.getRace() != null) district.setCounty(county.getRace());
        if (neighborhood.getRace() != null) district.setNeighbourhood(neighborhood.getRace());
        if (zip.getRace() != null) district.setZip(zip.getRace());
        return district;
    }

    public District getAge(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getAge() != null) district.setState(state.getAge());
        if (city.getAge() != null) district.setCity(city.getAge());
        if (county.getAge() != null) district.setCounty(county.getAge());
        if (neighborhood.getAge() != null) district.setNeighbourhood(neighborhood.getAge());
        if (zip.getAge() != null) district.setZip(zip.getAge());
        return district;
    }

    public District getArea(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getArea() != null) {
            AreaItem area = state.getArea();
            if (area.getLand() != null && area.getWater() != null) {
                district.setState(area);
            }
        }
        if (city.getArea() != null) {
            AreaItem area = city.getArea();
            if (area.getLand() != null && area.getWater() != null) {
                district.setCity(area);
            }
        }
        if (county.getArea() != null) {
            AreaItem area = county.getArea();
            if (area.getLand() != null && area.getWater() != null) {
                district.setCounty(area);
            }
        }
        if (neighborhood.getArea() != null) {
            AreaItem area = neighborhood.getArea();
            if (area.getLand() != null && area.getWater() != null) {
                district.setNeighbourhood(area);
            }
        }
        if (zip.getArea() != null) {
            AreaItem area = zip.getArea();
            if (area.getLand() != null && area.getWater() != null) {
                district.setZip(area);
            }
        }
        return district;

    }

    public District getGender(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea zip) {
        District district = new District();
        if (state.getGender() != null) district.setState(state.getGender());
        if (city.getGender() != null) district.setCity(city.getGender());
        if (county.getGender() != null) district.setCounty(county.getGender());
        if (neighborhood.getGender() != null) district.setNeighbourhood(neighborhood.getGender());
        if (zip.getGender() != null) district.setZip(zip.getGender());
        return district;
    }

    public District getPopulation(BaseArea state, BaseArea city, BaseArea county, BaseArea neighborhood, BaseArea
            zip) {
        District district = new District();
        if (state.getPopulation() != null) district.setState(state.getPopulation());
        if (city.getPopulation() != null) district.setCity(city.getPopulation());
        if (county.getPopulation() != null) district.setCounty(county.getPopulation());
        if (neighborhood.getPopulation() != null) district.setNeighbourhood(neighborhood.getPopulation());
        if (zip.getPopulation() != null) district.setZip(zip.getPopulation());
        return district;
    }


}
